package io.renren.utils;

import io.renren.entity.SocialSecurityPayDetailEntity;

/**
 * Created by zhangheng on 2017/7/30.
 */
public class SocialSecurityAreaBasicUtils {

    public static SocialSecurityPayDetailEntity updatePayDetail(SocialSecurityPayDetailEntity payDetail,
                                                                double yanglbasic, double yangldwbl, double yanglgrbl,
                                                                double shengybasic, double shengydwbl, double shengygrbl,
                                                                double gongsbasic, double gongsdwbl, double gongsgrbl,
                                                                double yilbasic, double yildwbl, double yilgrbl,
                                                                double shiybasic, double shiydwbl, double shiygrbl,
                                                                double totalbasic, double totaldwbl, double totalgrbl,
                                                                String empbasenum) {
        /**
         * 如果是已经有付款记录的社保明细记录，即表明该记录为已经结算过的记录，
         * 就修改补差字段的数据，同时修改补差缴付状态为未缴付
         */
        if (payDetail.getUnitPay() != null) {
            String sstype = payDetail.getSocialSecurityType();
            if ("socialSecurity01".equals(sstype)) {//养老保险
                payDetail = setBcInfo(payDetail, yanglbasic, yangldwbl, yanglgrbl, empbasenum);

            } else if ("socialSecurity02".equals(sstype)) {//生育保险
                payDetail = setBcInfo(payDetail, shengybasic, shengydwbl, shengygrbl, empbasenum);

            } else if ("socialSecurity03".equals(sstype)) {//工伤保险
                payDetail = setBcInfo(payDetail, gongsbasic, gongsdwbl, gongsgrbl, empbasenum);

            } else if ("socialSecurity04".equals(sstype)) {//医疗保险
                payDetail = setBcInfo(payDetail, yilbasic, yildwbl, yilgrbl, empbasenum);

            } else if ("socialSecurity05".equals(sstype)) {//失业保险
                payDetail = setBcInfo(payDetail, shiybasic, shiydwbl, shiygrbl, empbasenum);

            } else if ("socialSecurity06".equals(sstype)) {//五险合一
                payDetail = setBcInfo(payDetail, totalbasic, totaldwbl, totalgrbl, empbasenum);

            }
        } else {//如果没有付款记录，则表明数据没有结算，只需修改应缴字段即可
            String sstype = payDetail.getSocialSecurityType();
            if ("socialSecurity01".equals(sstype)) {//养老保险
                payDetail = setBcOriginInfo(payDetail, yanglbasic, yangldwbl, yanglgrbl, empbasenum);

            } else if ("socialSecurity02".equals(sstype)) {//生育保险
                payDetail = setBcOriginInfo(payDetail, shengybasic, shengydwbl, shengygrbl, empbasenum);

            } else if ("socialSecurity03".equals(sstype)) {//工伤保险
                payDetail = setBcOriginInfo(payDetail, gongsbasic, gongsdwbl, gongsgrbl, empbasenum);

            } else if ("socialSecurity04".equals(sstype)) {//医疗保险
                payDetail = setBcOriginInfo(payDetail, yilbasic, yildwbl, yilgrbl, empbasenum);

            } else if ("socialSecurity05".equals(sstype)) {//失业保险
                payDetail = setBcOriginInfo(payDetail, shiybasic, shiydwbl, shiygrbl, empbasenum);

            } else if ("socialSecurity06".equals(sstype)) {//五险合一
                payDetail = setBcOriginInfo(payDetail, totalbasic, totaldwbl, totalgrbl, empbasenum);

            }
        }
        return payDetail;
    }

    /**
     * 理论上社保缴付明细表的社保基数及比例跟个人社保基数一致，跟区域基数也一致。
     * 所以在计算的时候以缴付明细表的原始记录来处理
     *
     * @param payDetail 社保缴付明细原始数据
     * @param basic     区域基数
     * @param dwbl      区域单位比例
     * @param grbl      区域个人比例
     * @param basicflag 区域社保基数(0)个人社保基数(1)
     * @return
     */
    public static SocialSecurityPayDetailEntity setBcInfo(SocialSecurityPayDetailEntity payDetail, double basic, double dwbl, double grbl, String basicflag) {
        double dwbc;
        double grbc;
        if ("0".equals(basicflag)) {
            dwbc = basic * dwbl - payDetail.getUnitPayable();
            grbc = basic * grbl - payDetail.getPersonPayable();
            payDetail.setBaseNum(basic);
        } else {
            dwbc = payDetail.getBaseNum() * dwbl - payDetail.getUnitPayable();
            grbc = payDetail.getBaseNum() * grbl - payDetail.getPersonPayable();
        }
        payDetail.setUnitScale(dwbl);
        payDetail.setPersonScale(grbl);
        payDetail.setBalancePayableUnit(dwbc);
        payDetail.setBalancePayablePerson(grbc);
        payDetail.setBalancePayableState("0");
        return payDetail;
    }

    /**
     * 理论上社保缴付明细表的社保基数及比例跟个人社保基数一致，跟区域基数也一致。
     * 所以在计算的时候以缴付明细表的原始记录来处理
     *
     * @param payDetail 社保缴付明细原始数据
     * @param basic     区域基数
     * @param dwbl      区域单位比例
     * @param grbl      区域个人比例
     * @param basicflag 区域社保基数(0)个人社保基数(1)
     * @return
     */
    public static SocialSecurityPayDetailEntity setBcOriginInfo(SocialSecurityPayDetailEntity payDetail, double basic, double dwbl, double grbl, String basicflag) {
        if ("0".equals(basicflag)) {
            payDetail.setUnitScale(dwbl);
            payDetail.setPersonScale(grbl);
            payDetail.setBalancePayableUnit(basic * dwbl);
            payDetail.setBalancePayablePerson(basic * grbl);
            payDetail.setBaseNum(basic);
        } else {
            payDetail.setUnitScale(dwbl);
            payDetail.setPersonScale(grbl);
            payDetail.setBalancePayableUnit(payDetail.getBaseNum() * dwbl);
            payDetail.setBalancePayablePerson(payDetail.getBaseNum() * grbl);
        }
        return payDetail;
    }
}
